/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.bytedance.scenedemo.animatiom.sipnner;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.Component;

/**
 * ScrollScaleAnimator.
 *
 * @since 2021-06-17
 */
public class ScrollScaleAnimator extends PopupAnimator {
    private static final int SECOND = 2;
    /**
     * isOnlyScaleX
     */
    private boolean isOnlyScaleX = false;
    private float startAlpha = 0f;
    private float startScale = 0f;

    /**
     * ScrollScaleAnimator
     *
     * @param target         Component
     * @param popupAnimation PopupAnimation
     */
    public ScrollScaleAnimator(Component target, PopupAnimation popupAnimation) {
        super(target, popupAnimation);
    }

    @Override
    public void initAnimator() {
        getTargetView().setAlpha(startAlpha);
        getTargetView().setScaleX(startScale);
        if (!isOnlyScaleX) {
            getTargetView().setScaleY(startScale);
        }
        applyPivot(); // 设置参考点
    }

    private void applyPivot() {
        switch (getPopupAnimation()) {
            case ScrollAlphaFromLeft:
                getTargetView().setPivotX(0f);
                getTargetView().setPivotY((float) getTargetView().getHeight() / SECOND);
                break;
            case ScrollAlphaFromLeftTop:
                getTargetView().setPivotX(getTargetView().getWidth());
                getTargetView().setPivotY(0);
                break;
            case ScrollAlphaFromTop:
                getTargetView().setPivotX((float) getTargetView().getWidth() / SECOND);
                getTargetView().setPivotY(0f);
                break;
            case ScrollAlphaFromRightTop:
                getTargetView().setPivotX(getTargetView().getWidth());
                getTargetView().setPivotY(0f);
                break;
            case ScrollAlphaFromRight:
                getTargetView().setPivotX(getTargetView().getWidth());
                getTargetView().setPivotY((float) getTargetView().getHeight() / SECOND);
                break;
            case ScrollAlphaFromRightBottom:
                getTargetView().setPivotX(getTargetView().getWidth());
                getTargetView().setPivotY(getTargetView().getHeight());
                break;
            case ScrollAlphaFromBottom:
                getTargetView().setPivotX((float) getTargetView().getWidth() / SECOND);
                getTargetView().setPivotY(getTargetView().getHeight());
                break;
            case ScrollAlphaFromLeftBottom:
                getTargetView().setPivotX(0);
                getTargetView().setPivotY(getTargetView().getHeight());
                break;
            default:
                break;
        }
    }

    @Override
    public void animateShow() {
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(Popup.getAnimationDuration());
        animator.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                getTargetView().setAlpha(value);
                getTargetView().setScaleX(value);
                if (!isOnlyScaleX) {
                    getTargetView().setScaleY(value);
                }
            }
        });
        animator.start();
    }

    @Override
    public void animateDismiss() {
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(Popup.getAnimationDuration());
        animator.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);
        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                // 旋转imageView
                getTargetView().setAlpha(1 - value);
                getTargetView().setScaleX(1 - value);
                if (!isOnlyScaleX) {
                    getTargetView().setScaleY(1 - value);
                }
            }
        });
        animator.start();
    }

    public boolean isOnlyScaleX() {
        return isOnlyScaleX;
    }

    public void setOnlyScaleX(boolean onlyScaleX) {
        isOnlyScaleX = onlyScaleX;
    }
}
